package com.punai.monthanalysis.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.core.domain.BaseEntity;
import com.punai.externalccb.constants.CCBConstants;
import com.punai.externalccb.domain.YdCbfeeiteminfoStandard;
import com.punai.externalccb.domain.YdSummary;
import com.punai.externalccb.mapper.YdSummaryMapper;
import com.punai.monthanalysis.constants.YdcbfxConstants;
import com.punai.monthanalysis.domain.ProYdcbfxGcjdcbkhsjfxb;
import com.punai.monthanalysis.mapper.ProYdcbfxGcjdcbkhsjfxbMapper;
import com.punai.monthanalysis.service.IProYdcbfxGcjdcbkhsjfxbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 工程阶段成本考核数据分析Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-19
 */
@Service
public class ProYdcbfxGcjdcbkhsjfxbServiceImpl extends ServiceImpl<ProYdcbfxGcjdcbkhsjfxbMapper,ProYdcbfxGcjdcbkhsjfxb> implements IProYdcbfxGcjdcbkhsjfxbService
{
    @Autowired
    private ProYdcbfxGcjdcbkhsjfxbMapper proYdcbfxGcjdcbkhsjfxbMapper;
    @Autowired
    private YdSummaryMapper ydSummaryMapper;


    @Override
    public List<? extends BaseEntity> getTabsDataList(String proId,String monthId,List<YdCbfeeiteminfoStandard> list,List<YdCbfeeiteminfoStandard> categorysList) {
        List<String> names = new ArrayList<>();
        for (YdcbfxConstants.CBKH_SJ_FXB value : YdcbfxConstants.CBKH_SJ_FXB.values()) {
            names.add(value.getName());
        }
        List<YdSummary> ydSummaryList = ydSummaryMapper.selectList(
                Wrappers.<YdSummary>lambdaQuery()
                        .eq(YdSummary::getProId, proId)
                        .eq(YdSummary::getPid,0)
                        .eq(YdSummary::getYdDate,monthId)
                        .eq(YdSummary::getStagetype, CCBConstants.CCB_TYPE.YDCBFX.getCode())
                        .in(YdSummary::getName, names)
        );
        Map<String, YdSummary> collect = ydSummaryList.stream().collect(Collectors.toMap(YdSummary::getName, Function.identity(), (v1, v2) -> v2));
        List<ProYdcbfxGcjdcbkhsjfxb> resultList = new ArrayList<>();
        for (YdcbfxConstants.CBKH_SJ_FXB value : YdcbfxConstants.CBKH_SJ_FXB.values()) {
            String name = value.getName();
            ProYdcbfxGcjdcbkhsjfxb bo = new ProYdcbfxGcjdcbkhsjfxb();
            YdSummary ydSummary = collect.get(name);
            bo.setId(IdUtil.fastUUID());
            bo.setItemName(name);
            if (ydSummary != null) {
                bo.setYs(ydSummary.getMonthCz());
                bo.setSj(ydSummary.getMonthCb());
                bo.setJce(ydSummary.getMonthLrTotal());
                bo.setJcl(ydSummary.getMonthLrl());
            }
            resultList.add(bo);
        }
        return resultList;
    }
}
